package com.lizhenhua.fmmall.service.serviceImpl;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.lizhenhua.fmmall.entity.ShoppingCart;
import com.lizhenhua.fmmall.entity.ShoppingCartVo;
import com.lizhenhua.fmmall.mapper.ShoppingCartMapper;
import com.lizhenhua.fmmall.service.ShopCarService;
import com.lizhenhua.fmmall.vo.ResStatus;
import com.lizhenhua.fmmall.vo.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.logging.SimpleFormatter;

@Service
public class ShopCarServiceImpl implements ShopCarService {
    @Autowired
    private ShoppingCartMapper cartMapper;
    private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    @Autowired
    private ObjectMapper objectMapper;

    @Override
    public ResultVo addShopCart(ShoppingCart cart) {
        cart.setCartTime(sdf.format(new Date()));
        int insert = cartMapper.insert(cart);
        if (insert > 0) {
            return new ResultVo(ResStatus.OK, "success", null);
        } else {
            return new ResultVo(ResStatus.OK, "success", null);

        }
    }

    @Transactional(propagation = Propagation.SUPPORTS)
    public ResultVo selectShopListById(String uId) {
        List<ShoppingCartVo> cartVos = cartMapper.selectShopListById(uId);


        return new ResultVo(ResStatus.OK, "success", cartVos);
    }

    @Override
    public ResultVo updateCartNumByCartId(String cartNum, Integer cartId) {
        int i = cartMapper.updateCartNumByCartId(cartNum, cartId);
        if (i > 0) {
            return new ResultVo(ResStatus.OK, "success", i);
        } else {
            return new ResultVo(ResStatus.NO, "fail", null);
        }

    }

    @Override
    public ResultVo listShopListByCids(String cids) {
//        Example example = new Example(ShoppingCartVo.class);
//        Example.Criteria criteria = example.createCriteria();
//        criteria.andIn("cartId",cids); // 这一行必须支持迭代
//// 这里只能返回 List<ShoppingCart> 所以用这个不可以  因为我们需要图片单价等  不能满足我们要求
//        List<ShoppingCart> shoppingCarts = cartMapper.selectByExample(example);
//        List<ShoppingCartVo> list = cartMapper.selectShopListById()

        // 分割得到数组
        String[] arr = cids.split(",");

        // 强行转换为Integer 数组
        List<Integer> cartIds = new ArrayList<>();

        for (int i = 0; i < arr.length; i++) {
            cartIds.add(Integer.parseInt(arr[i]));
        }
        List<ShoppingCartVo> list = cartMapper.selectShopListByCartId(cartIds);

        ResultVo resultVo = new ResultVo(ResStatus.OK, "success", list);

        return resultVo;
    }
}
